import scenejson from "./scene.json";

import b64 from 'js-base64'

let aa = {
  data() {
    return {};
  },
  methods: {
    earthopt() {
      earth.xbsjFromJSON(scenejson);
      var tileset = earth.sceneTree.$refs.gg.czmObject;
      let viewer = earth.czm.viewer
      tileset.debugShowContentBoundingVolume = true;

      this.ttt = tileset

      // 点击修改3dtiles颜色，使用坐标拾取
      const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);

      handler.setInputAction((target) => {
        // 判断点击的元素
        const feature = viewer.scene.pick(target.position);
        // if (feature instanceof Cesium.Cesium3DTileFeature) {
        //   feature.color = Cesium.Color.RED;
        // }
        // console.log(feature.getProperty('Longitude'));


        feature.primitive.color = Cesium.Color.RED;
        console.log(feature);
        console.log(feature.primitive);
        console.log(feature.primitive.color);
        console.log(feature);
        console.log(target.position);


        var boundingVolume = feature.content.tile.boundingVolume;
        // 创建包围盒外边框
        // var boundingBox = boundingVolume.boundingBox;
        // var boxEntity = viewer.entities.add({
        //     position: boundingBox.center,
        //     box: {
        //         dimensions: new Cesium.Cartesian3(
        //             boundingBox.maximum.x - boundingBox.minimum.x,
        //             boundingBox.maximum.y - boundingBox.minimum.y,
        //             boundingBox.maximum.z - boundingBox.minimum.z
        //         ),
        //         material: Cesium.Color.RED.withAlpha(0.5)
        //     }
        // });

        // 创建包围盒外边框
        // var boundingSphere = boundingVolume.boundingSphere;
        // var sphereEntity = viewer.entities.add({
        //     position: boundingSphere.center,
        //     ellipsoid: {
        //         radii: new Cesium.Cartesian3(
        //             boundingSphere.radius,
        //             boundingSphere.radius,
        //             boundingSphere.radius
        //         ),
        //         material: Cesium.Color.BLUE.withAlpha(0.5)
        //     }
        // });


        // 获取根瓦片的包围盒的半轴
        var halfAxes = boundingVolume.boundingVolume.halfAxes;
        // 创建包围盒的半轴
        var axesInstance = new Cesium.GeometryInstance({
            geometry: new Cesium.BoxGeometry({
                minimum: new Cesium.Cartesian3(-1, -1, -1),
                maximum: new Cesium.Cartesian3(1, 1, 1)
            }),
            modelMatrix: Cesium.Matrix4.fromRotationTranslation(halfAxes, boundingVolume.boundingVolume.center),
            attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromCssColorString('#f005'))
            },
            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
        });

        let res = viewer.scene.primitives.add(new Cesium.Primitive({
            geometryInstances: axesInstance,
            appearance: new Cesium.PerInstanceColorAppearance()
        }));

        viewer.entities.add({
          position: boundingVolume.boundingVolume.center,
          label: {
            text: feature.content.tile.extras.comment.split("=")[1],
            showBackground: true,
            pixelOffset: new Cesium.Cartesian2(0, -100)
          }
        })

        // b64.decode()





        console.log(res);




      }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

      console.log(tileset._tileset);
    },
  },
};

export default aa;
